package com.hh.system.service.impl;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.hh.baseframework.util.Check;
import com.hh.baseframework.util.Convert;
import com.hh.baseframework.util.MessageException;
import com.hh.baseframework.util.dto.PageRange;
import com.hh.hibernate.dao.inf.IHibernateDAO;
import com.hh.hibernate.util.base.BaseOneEntity;
import com.hh.hibernate.util.dto.HQLParamList;
import com.hh.hibernate.util.dto.PagingData;

public class BaseService<T extends BaseOneEntity> extends Base<T> {
	@Autowired
	protected IHibernateDAO<T> dao;

	public PagingData<T> queryPagingData(T entity, PageRange pageRange) {
		return dao.queryPagingData(this.getGenericType(0), new HQLParamList(),
				pageRange);
	}

	public PagingData<T> queryPagingData(T entity, PageRange pageRange,
			HQLParamList hqlParamList) {
		return dao.queryPagingData(this.getGenericType(0), hqlParamList,
				pageRange);
	}

	public T save(T entity) throws MessageException {
		if (Check.isEmpty(entity.getId())) {
			dao.createEntity(entity);
		} else {
			dao.updateEntity(entity);
		}
		return entity;
	}

	public void update(String id, String key, Object value) {
		dao.updateProperty(this.getGenericType(0), id, key, value);
	}

	public T findObjectById(String id) {
		T entity = dao.findEntityByPK(this.getGenericType(0), id);
		return entity;
	}

	public T findObjectByProperty(String property, Object value) {
		T entity = dao.findEntity(this.getGenericType(0),
				Restrictions.eq(property, value));
		return entity;
	}

	public List<T> queryListByProperty(String property, Object value) {
		List<T> entityList = dao.queryList(this.getGenericType(0),
				Restrictions.eq(property, value));
		return entityList;
	}

	public List<T> queryAllList() {
		List<T> entityList = dao.queryList(this.getGenericType(0));
		return entityList;
	}

	public List<T> queryListByIds(List<String> ids) {
		return dao.queryList(this.getGenericType(0), "id", ids);
	}
	
	public List<T> queryList(List<Object> paramList) {
		return dao.queryList(this.getGenericType(0), paramList);
	}

	public void deleteByIds(String ids) {
		List<String> idList = Convert.strToList(ids);
		if (!Check.isEmpty(idList)) {
			dao.deleteEntity(this.getGenericType(0), "id", idList);
		}
	}

	public void deleteByProperty(String property, Object value) {
		if (value instanceof List) {
			dao.deleteEntity(this.getGenericType(0), property, (List) value);
		} else {
			dao.deleteEntity(this.getGenericType(0), property, value);
		}

	}

	public void updateOrder(String id1, String order1, String id2, String order2) {
		if (Check.isNoEmpty(order2) && Check.isNoEmpty(order1)) {
			dao.updateEntity("update " + this.getGenericType(0).getName()
					+ " o set o.order=? where o.id=?", new Object[] { order1,
					id2 });
			dao.updateEntity("update " + this.getGenericType(0).getName()
					+ " o set o.order=? where o.id=?", new Object[] { order2,
					id1 });
		} else {
			T entity1 = dao.findEntity(this.getGenericType(0),
					Restrictions.eq("id", id1));
			String order1_ = entity1.getOrder();
			T entity2 = dao.findEntity(this.getGenericType(0),
					Restrictions.eq("id", id2));
			String order2_ = entity2.getOrder();
			dao.updateEntity("update " + this.getGenericType(0).getName()
					+ " o set o.order=? where o.id=?", new Object[] { order1_,
					id2 });
			dao.updateEntity("update " + this.getGenericType(0).getName()
					+ " o set o.order=? where o.id=?", new Object[] { order2_,
					id1 });
		}
	}
	
	public void createEntity(T entity) {
		dao.createEntity(entity);
	}

}
